﻿using ElasticsearchCore.ElasticSearch.Config;
using ElasticsearchCoreEs.ElasticsearchCoreElasticSearchs.Contents;
using ElasticsearchCoreEs.ElasticsearchCoreElasticSearchs.Contents.Dto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;

namespace ElasticsearchCore.Api.Controllers
{
    public class SeachController : Controller
    {
        private readonly IContentService _IContentService;
        public SeachController(IContentService IContentService)
        {
            _IContentService = IContentService;
        }
        [Route("api/Seach/get")]
        public string get()
        {
            var result = new List<Content>();
            try
            {
                Stopwatch sb = new Stopwatch();
                //sb.Start();
                //_IContentService.BlukIndex(Contents, "index_cus");
                //sb.Stop();
                //Console.WriteLine("写入成功:" + sb.ElapsedMilliseconds.ToString());
                //复杂查询
                var request = new RequestContentDto
                {
                    PageSize = 50,
                    SearchKey = "",

                };

                #region 高亮

                var highlightConfig = new HighlightConfig<Content>
                {
                    Tag = "i",
                    HighlightConfigExpression = new List<Expression<Func<Content, object>>>
                {
                    x=>x.Title
                }
                };
                request.HighlightConfigEntity = highlightConfig;
                if (_IContentService != null)
                {
                    Stopwatch sb2 = new Stopwatch();
                    sb2.Start();
                    var data = _IContentService.Search(1, request.PageSize, request);
                    sb2.Stop();
                    result.AddRange(data.Contents);
                    Console.WriteLine("查询数量" + data.Contents.Count + "----查询时间：" + sb2.ElapsedMilliseconds);
                    return  "查询数量" + data.Contents.Count + "----查询时间：" + sb2.ElapsedMilliseconds + "----总数：" + data.Records;
                    //Console.WriteLine(Newtonsoft.Jschon.JsonConvert.SerializeObject(data.Contents) + "时间：" + sb2.ElapsedMilliseconds);
                }
                else
                {
                    Console.WriteLine("null");
                }

                ////删除
                //_IContentService.DeleteByQuery("123852");

                ////更新
                //_IContentService.UpdateByKey("123754", new Content
                //{
                //    Key = "123"
                //});

                #endregion 高亮
            }
            catch (Exception e)
            {

                return $"异常:" + e.Message + e.InnerException.Message;
            }
            return "";
        }
    }
}